package com.uidp.search;

import com.acsior.dac.exeption.DataAccessException;
import com.acsior.sbc.exception.BusinessException;
import com.acsior.sbc.exception.SearchException;
import com.acsior.sbc.impl.hibernate.SearchHibernateBusinessObject;
import com.uidp.domain.Usergroups;
import com.uidp.domain.Users;
import com.uidp.hibernatelayer.UsersDAO;
import static com.uidp.hibernatelayer.UsersDAO.USERNAME;
import static com.uidp.hibernatelayer.UsersDAO.FAMILY;
import static com.uidp.hibernatelayer.UsersDAO.FIRSTNAME;
import static com.uidp.hibernatelayer.UsersDAO.EGN;
import static com.uidp.hibernatelayer.UsersDAO.USERGROUP;
import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.hibernate.criterion.Restrictions.eq;
import com.uidp.hibernatelayer.UsersVO;

public class UserSearch extends
		SearchHibernateBusinessObject<UsersVO, UsersDAO, Users> {
	
	public UserSearch() throws BusinessException {
		super();

	}

	private Users userCriteria;

	/**
	 * Конструктор
	 * 
	 * @param username
	 * 
	 * @throws BusinessException
	 */
	public UserSearch(String username) throws BusinessException {
		this.userCriteria.setUsername(username);
	}

	/**
	 * Конструктор
	 * 
	 * @param username
	 * 
	 * @throws BusinessException
	 */
	public UserSearch(String username, String firstName, String family,
			String egn, Usergroups us) throws BusinessException {
		this.userCriteria.setUsername(username);
		this.userCriteria.setFirstName(firstName);
		this.userCriteria.setFamily(family);
		this.userCriteria.setEgn(egn);
		this.userCriteria.setUsergroups(us);

	}

	/** @see SearchHibernateBusinessObject#buildCriteria() */
	@Override
	protected void buildCriteria() throws SearchException {
		super.buildCriteria();
		if (isNotEmpty(getUserCriteria().getUsername())) {
			getCriteria().add(eq(USERNAME, getUserCriteria().getUsername()));
		}
		if (isNotEmpty(getUserCriteria().getFirstName())) {
			getCriteria().add(eq(FIRSTNAME, getUserCriteria().getFirstName()));
		}
		if (isNotEmpty(getUserCriteria().getFamily())) {
			getCriteria().add(eq(FAMILY, getUserCriteria().getFamily()));
		}
		if (isNotEmpty(getUserCriteria().getEgn())) {
			getCriteria().add(eq(EGN, getUserCriteria().getEgn()));
		}
		if (isNotEmpty(getUserCriteria().getUsergroups().getGroupname())) {
			try {
				getCriteria().add(eq(USERGROUP, getUserCriteria().getUsergroups().findData()));
			} catch (DataAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/** @see SearchObject#createResultObject(Data) */
	@Override
	protected Users createResultObject(UsersVO d) throws BusinessException {
		return new Users(d);
	}

	/** @see WorkerObject#getDAOClass() */
	@Override
	protected Class<UsersDAO> getDAOClass() {
		return UsersDAO.class;
	}

	/** @see SearchObject#initialize() */
	@Override
	protected final void initialize() throws BusinessException {
		super.initialize();

		this.userCriteria = new Users();
	}

	/** @return userCriteria */
	private Users getUserCriteria() {
		return this.userCriteria;
	}

}
